home *** CD-ROM | disk | FTP | other *** search
- Path: prodigy.com!usenet
- From: EWTE97A@prodigy.com (Gregory Gibson)
- Newsgroups: comp.lang.c++
- Subject: BC4.5 won't compile it; GNU will [templates]
- Date: 15 Mar 1996 05:48:36 GMT
- Organization: Prodigy Services Company 1-800-PRODIGY
- Distribution: world
- Message-ID: <4ib0bk$16cg@usenetz1.news.prodigy.com>
- NNTP-Posting-Host: innugap2-int.news.prodigy.com
- X-Newsreader: Version 1.2
-
- //GNU compiles this and it runs. BC 4.5 does not compile it.
- //And simple errors like (a<>b) instead of (a!=b) are ignored?
- //What is wrong with BC 4.5?
-
- // I APPRECIATE THE TIME INVESTMENT YOU MAKE!!!
-
- // BC 4.5 does not seem to recognize the global 'pool'
-
- //=================================================================
- #include <iostream.h>
- #include <stdio.h>
- #include <string.h> /* strcpy prototype */
-
- #define TSIZE 45
- typedef char * string;
-
- //==================================================================
- class film { // I put this here because everything depends on it.
- char title[TSIZE];
- public:
- film() { strcpy(title,""); }
- film(string s) { strcpy(title,s); }
- operator string() { return title; }
- friend ostream& operator<< (ostream& out, film& z);
- };
-
- ostream& operator<< (ostream& out, film& z) { return (out << z.title); }
-
- //==================================================================
- //CIRCULAR SORTED LINKED LIST CLASS TEMPLATE
- template <class GenTy>
- class list {
- GenTy data;
- public:
- list *next;
-
- list(string s="zzzzzzzzzz") {
- data = s;
- next = this;
- }
-
- GenTy getdata() { return data; }
-
- list<GenTy>* getptr(string s){
- list<GenTy>* temp;
- temp=pool.new_node();
- temp->data=s;
- return temp;
- }
-
- int empty() {return this==this->next;}
-
- void find(list<GenTy>* &, list<GenTy>* &,const string &);
-
- void add(const string &);
-
- void del(const string &);
-
- void print();
- };
-
- template <class GenTy> //FIND
- void list<GenTy>::find(list<GenTy>* & p, list<GenTy>* & c,const string &
- s) {
- while (strcmp(c->data,s) < 0) { p=c; c=c->next;}
- }
-
- template <class GenTy> //ADD
- void list<GenTy>::add(const string & s) {
- cout << "adding: " << s << endl;
- list<GenTy> *newptr=getptr(s), *p=this, *c=this->next;
- list<GenTy>::find(p,c,s);
- newptr->next=p->next;
- p->next=newptr;
- }
-
- template <class GenTy> //DEL
- void list<GenTy>::del(const string & s) {
- cout << "attempting to delete: " << s << endl;
- list<GenTy> *p=this, *c=this->next;
- list<GenTy>::find(p,c,s);
- if (strcmp(c->data,s) != 0)
- cout << s << " not in list." << endl;
- else {
- p->next=c->next;
- pool.recycle(c);
- }
- }
-
- template <class GenTy> //PRINT
- void list<GenTy>::print() {
- if (list<GenTy>::empty()) cout << "Empty list! " << endl;
- else {
- list<GenTy> *cur=this;
- while (cur->next!=this) {
- cur=cur->next;
- cout << cur->data << endl;
- } } }
- //=======================================================
- // MEMORY MANAGER CLASS TEMPLATE (PARTIAL)
-
- template <class N> // I changed N to be the same type used to
- // instantiate list.
- class memman {
- typedef list<N> node;
- node *pool_next, *pool_end, *r_bin;
-
- void fill_pool(int i) { // added void
- pool_next = new node[i];
- pool_end = pool_next+(i-1);
- }
-
- public:
- memman() { fill_pool(4); r_bin=NULL; }
- memman(int i) { fill_pool(i); r_bin=NULL; }
-
- list<N> * new_node(); // return a node from r_bin or the pool
- void recycle(list<N> *); // put deallocated node on head of list
- void show_bin(); // prints contents of recycle bin
- };
-
- template <class N> //NEW_NODE
- list<N>* memman<N>::new_node() {
- node * temp;
- if (r_bin!=NULL) {
- temp=r_bin; r_bin=r_bin->next;
- return temp; }
- else {
- if (pool_next==pool_end) fill_pool(4);
- temp=pool_next; pool_next+=1; return temp;}
- }
-
- template <class N> //RECYCLE
- void memman<N>::recycle(list<N>* divorced) {
- divorced->next=r_bin;
- r_bin=divorced;
- }
-
- template <class N> //SHOW_BIN
- void memman<N>::show_bin() {
- node * temp=r_bin;
- while (temp != NULL) {
- cout << temp->data << endl;
- temp=temp->next;}
- }
-
- //===========================================================
-
- memman<film> pool; // This is the key to making everything work together.
-
-
- void main() {
-
- list<film> movies;
- char input[TSIZE];
-
- puts("Enter first movie title:");
- while (gets(input) != NULL && input[0] != '\0') {
-
- if (input[strlen(input)-1] == '-') { // if last character is
- // "-" delete
- input[strlen(input)-1]='\0'; //last char is "-" remove it
- movies.del(input);
- }
- else movies.add(input);
- puts("Enter next movie title (`title-' deletes; linefeed to stop):");
- }
- movies.print();
- }
-
-
-
-
-